<?php
define('APP_START_TIME',microtime(true));
$basePath = substr($_SERVER['SCRIPT_FILENAME'],0,strrpos($_SERVER['SCRIPT_FILENAME'],'/'));
if(empty($basePath)){
    $basePath = $_ENV['PWD'];
}
$childrenPath = str_replace($_SERVER['DOCUMENT_ROOT'],'',$basePath);
$baseUrl = 'http://'.$_SERVER['HTTP_HOST'].$childrenPath;
if(empty($_SERVER['DOCUMENT_ROOT'])){
    $baseUrl = 'http://'.$_SERVER['HTTP_HOST'];
}
define('BASE_URL',$baseUrl);
define('BASE_PATH',$basePath);


$config  = require_once($config);

define('D_DEBUG',$config['debug']['switch']);

/**
 * 由名称获取路径
 * @param string $name
 */
function nameToPath($name,$extension='.php'){
    $name = trim($name);
    $array_name = explode('.',$name);
    $path = '';
    foreach($array_name as $v){
        $v = strtolower($v);
        $path .= '/'.$v;
    }
    $path = $path.$extension;
    return $path;
}

function autoloadClass($class){
    global $config;
    $includePath = $config['includePath'];
    if(count($includePath)>0){
        foreach($includePath as $k=>$v){
            if(substr($v,-1)=='*'){
                $path = $config['base']['root'].'/'.str_replace('.','/',substr($v,0,strlen($v)-2));
                //echo PHP_EOL.BASE_PATH.$path.PHP_EOL;
                $pathName = $path.'/'.strtolower($class).'.php';
                if(file_exists($pathName)){
                    include($path.'/'.strtolower($class).'.php');
                }
            }
        }
    }
}

function shutdown(){
	if(D_DEBUG){
		Debug::buildTrackInfo();
	}
}
register_shutdown_function('shutdown');


spl_autoload_register('autoloadClass');

